package shark.picture.utils.other;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.sl.usermodel.VerticalAlignment;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;

import java.awt.*;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author jianxin.wei
 * @description
 * @date 2025/9/5 15:50
 */
@Slf4j
public class Wps {

    public static void main(String[] args) {
        try {
            String str = DateUtil.format(DateUtil.offsetMonth(DateUtil.date(), -1), "yyyyMM");
            System.out.println(str.substring(4));
//            JSONObject param = new JSONObject();
//            param.set("type", 3);
//            gdfaPDF(param);
//            gdfaSign(param);
//            yewjSign();
//            sddPDF();
//            gyyddjbPDF();
//            sddSign();
        } catch (Exception e) {
            log.info("", e);
        }
    }
    public static void gyyddjbPDF() throws Exception {
        String inputPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\高压用电登记表模板.pdf";
        String outPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\高压用电登记表-1.pdf";

        byte[] inputData = FileUtil.readBytes(inputPath);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PdfReader reader = new PdfReader(inputData);
        PdfStamper stamper = new PdfStamper(reader, outputStream);
        AcroFields form = stamper.getAcroFields();
        BaseFont bf = BaseFont.createFont("Font/ybst.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        form.addSubstitutionFont(bf);

        List<String> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list.add("我是测试我是");
        }

        Map<String, Object> data = new HashMap<>();
        data.put("fill_1", list.remove(0));
        data.put("fill_2", list.remove(0));
        data.put("fill_3", list.remove(0));
        data.put("fill_4", list.remove(0));
        data.put("fill_5", list.remove(0));
        data.put("fill_6", list.remove(0));
        data.put("filp'.;l_7", list.remove(0));
        data.put("fill_8", list.remove(0));
        data.put("fill_9", list.remove(0));
        data.put("fill_10", list.remove(0));
        data.put("fill_11", list.remove(0));
        data.put("fill_12", list.remove(0));
        data.put("fill_13", list.remove(0));
        data.put("fill_14", list.remove(0));
        data.put("fill_15", list.remove(0));
        data.put("fill_16", list.remove(0));
        data.put("fill_17", list.remove(0));
        data.put("fill_18", list.remove(0));
        data.put("fill_19", list.remove(0));
        data.put("fill_20", list.remove(0));
        data.put("fill_21", list.remove(0));
        data.put("fill_22", list.remove(0));
        data.put("fill_23", list.remove(0));
        data.put("fill_24", list.remove(0));

        data.put("t_1", "0");
        data.put("t_2", "0");
        data.put("t_3", "0");
        data.put("t_4", "0");
        data.put("t_5", "0");
        data.put("t_6", "0");
        data.put("t_7", "0");
        data.put("t_8", "0");


        for (String key : data.keySet()) {
            form.setField(key, data.get(key).toString());
        }



        stamper.setFormFlattening(true);
        stamper.close();
        outputStream.writeTo(Files.newOutputStream(Paths.get(outPath)));
        outputStream.close();
        reader.close();

        System.out.println("===============PDF导出成功=============");
    }

    public static void sddSign() throws Exception {
        String inputPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\送电单模板-1.pdf";
        String outPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\送电单模板-1-签字.pdf";
        String imagePath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\t.png";

        byte[] inputData = FileUtil.readBytes(inputPath);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PdfReader reader = new PdfReader(inputData);
        PdfStamper stamper = new PdfStamper(reader, outputStream);
        BaseFont bf = BaseFont.createFont("Font/ybst.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);


        Date date = new Date();
        String now = DateUtil.format(date, "yyyy年MM月dd日");

        PdfContentByte canvas = stamper.getOverContent(1);
        canvas.beginText();
        canvas.setFontAndSize(bf, 12); // 字体和大小
        canvas.setTextMatrix(432, 255);
        canvas.showText(now);
        canvas.endText();

        Image img = Image.getInstance(imagePath);
        PdfContentByte content = stamper.getOverContent(1);
        img.setAbsolutePosition(120, 275); // 坐标
        img.scaleToFit(70, 30); // 缩放
        content.addImage(img);


        stamper.setFormFlattening(true);
        stamper.close();
        reader.close();
        outputStream.writeTo(Files.newOutputStream(Paths.get(outPath)));
        System.out.println("===============PDF签名成功=============");
    }

    public static void sddPDF() throws Exception {
        String inputPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\送电单模板.pdf";
        String outPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\送电单模板-1.pdf";

        byte[] inputData = FileUtil.readBytes(inputPath);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PdfReader reader = new PdfReader(inputData);
        PdfStamper stamper = new PdfStamper(reader, outputStream);
        AcroFields form = stamper.getAcroFields();
        BaseFont bf = BaseFont.createFont("Font/ybst.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        form.addSubstitutionFont(bf);

        List<String> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list.add("我是测试我是");
        }

        Map<String, Object> data = new HashMap<>();
        data.put("fill_1", list.remove(0));
        data.put("fill_2", list.remove(0));
        data.put("fill_3", list.remove(0));
        data.put("fill_4", list.remove(0));
        data.put("fill_5", list.remove(0));
        data.put("fill_6", list.remove(0));
        data.put("fill_7", list.remove(0));
        data.put("fill_8", list.remove(0));
        data.put("fill_9","                      "+ list.remove(0));
        data.put("fill_10","                 "+ list.remove(0));
        data.put("fill_11","                 "+ list.remove(0));
        data.put("fill_12","                 "+ list.remove(0));
        data.put("fill_13","                 "+ list.remove(0));
        data.put("fill_14","                 "+ list.remove(0));
        data.put("fill_15","                 "+ list.remove(0));
        data.put("fill_16","                 "+ list.remove(0));
        data.put("fill_17","                 "+ list.remove(0));
        data.put("fill_18", list.remove(0));
        data.put("fill_19", list.remove(0));
        data.put("fill_20", list.remove(0));

        for (String key : data.keySet()) {
            form.setField(key, data.get(key).toString());
        }



        stamper.setFormFlattening(true);
        stamper.close();
        outputStream.writeTo(Files.newOutputStream(Paths.get(outPath)));
        outputStream.close();
        reader.close();

        System.out.println("===============PDF导出成功=============");
    }

    public static void yewjSign() throws Exception {
        String inputPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\缴费通知单模板-1.pdf";
        String outPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\缴费通知单模板-1-签字.pdf";
        String imagePath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\t.png";

        byte[] inputData = FileUtil.readBytes(inputPath);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PdfReader reader = new PdfReader(inputData);
        PdfStamper stamper = new PdfStamper(reader, outputStream);
        BaseFont bf = BaseFont.createFont("Font/ybst.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);


        Date date = new Date();
        String now = DateUtil.format(date, "yyyy年MM月dd日");

        PdfContentByte canvas = stamper.getOverContent(1);
        canvas.beginText();
        canvas.setFontAndSize(bf, 11); // 字体和大小
        canvas.setTextMatrix(432, 385);
        canvas.showText(now);
        canvas.endText();

        Image img = Image.getInstance(imagePath);
        PdfContentByte content = stamper.getOverContent(1);
        img.setAbsolutePosition(185, 374); // 坐标
        img.scaleToFit(70, 30); // 缩放
        content.addImage(img);


        stamper.setFormFlattening(true);
        stamper.close();
        reader.close();
        outputStream.writeTo(Files.newOutputStream(Paths.get(outPath)));
        System.out.println("===============PDF签名成功=============");
    }

    public static void yewjPDF() throws Exception {
        String inputPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\缴费通知单模板.pdf";
        String outPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\缴费通知单模板-1.pdf";

        byte[] inputData = FileUtil.readBytes(inputPath);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PdfReader reader = new PdfReader(inputData);
        PdfStamper stamper = new PdfStamper(reader, outputStream);
        AcroFields form = stamper.getAcroFields();
        BaseFont bf = BaseFont.createFont("Font/ybst.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        form.addSubstitutionFont(bf);

        List<String> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list.add("HH");
        }

        Map<String, Object> data = new HashMap<>();
        data.put("fill_1", list.remove(0));
        data.put("fill_2", list.remove(0));
        data.put("fill_3", list.remove(0));
        data.put("fill_4", list.remove(0));
        data.put("fill_5", list.remove(0));
        data.put("fill_6", list.remove(0));
        data.put("fill_7", list.remove(0));
        data.put("fill_8", list.remove(0));
        data.put("fill_9", list.remove(0));
        data.put("fill_10", list.remove(0));
        data.put("fill_11", list.remove(0));
        data.put("fill_12", list.remove(0));

        for (String key : data.keySet()) {
            form.setField(key, data.get(key).toString());
        }

        stamper.setFormFlattening(true);
        stamper.close();
        outputStream.writeTo(Files.newOutputStream(Paths.get(outPath)));
        outputStream.close();
        reader.close();

        System.out.println("===============PDF导出成功=============");
    }

    public static void gdfaSign(JSONObject param) throws Exception {
        int type = param.getInt("type");
        String inputPath = "";
        String outPath = "";
        if (type == 1) {
            inputPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\供电方案答复单-1.pdf";
            outPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\供电方案答复单-1-签字.pdf";
        } else if (type == 2) {
            inputPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\供电方案答复单-2.pdf";
            outPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\供电方案答复单-2-签字.pdf";
        } else if (type == 3) {
            inputPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\供电方案答复单-3.pdf";
            outPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\供电方案答复单-3-签字.pdf";
        } else {
            System.out.println("不支持的模板类型");
            return;
        }
        String imagePath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\t.png";

        byte[] inputData = FileUtil.readBytes(inputPath);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PdfReader reader = new PdfReader(inputData);
        PdfStamper stamper = new PdfStamper(reader, outputStream);
        BaseFont bf = BaseFont.createFont("Font/ybst.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);


        Date date = new Date();
        String year = DateUtil.format(date, "yyyy");
        String month = DateUtil.format(date, "MM");
        String day = DateUtil.format(date, "dd");

        if (type == 1) {
            PdfContentByte canvas = stamper.getOverContent(1);
            canvas.beginText();
            canvas.setFontAndSize(bf, 9); // 字体和大小
            canvas.setTextMatrix(110, 49);
            canvas.showText(year);
            canvas.endText();

            PdfContentByte canvas1 = stamper.getOverContent(1);
            canvas1.beginText();
            canvas1.setFontAndSize(bf, 9); // 字体和大小
            canvas1.setTextMatrix(138, 49);
            canvas1.showText(month);
            canvas1.endText();

            PdfContentByte canvas2 = stamper.getOverContent(1);
            canvas2.beginText();
            canvas2.setFontAndSize(bf, 9); // 字体和大小
            canvas2.setTextMatrix(155, 49);
            canvas2.showText(day);
            canvas2.endText();


            Image img = Image.getInstance(imagePath);
            PdfContentByte content = stamper.getOverContent(1);
            img.setAbsolutePosition(115, 60); // 坐标
            img.scaleToFit(80, 35); // 缩放
            content.addImage(img);
        } else {
            PdfContentByte canvas = stamper.getOverContent(2);
            canvas.beginText();
            canvas.setFontAndSize(bf, 9); // 字体和大小
            canvas.setTextMatrix(110, 570);
            canvas.showText(year);
            canvas.endText();

            PdfContentByte canvas1 = stamper.getOverContent(2);
            canvas1.beginText();
            canvas1.setFontAndSize(bf, 9); // 字体和大小
            canvas1.setTextMatrix(138, 570);
            canvas1.showText(month);
            canvas1.endText();

            PdfContentByte canvas2 = stamper.getOverContent(2);
            canvas2.beginText();
            canvas2.setFontAndSize(bf, 9); // 字体和大小
            canvas2.setTextMatrix(155, 570);
            canvas2.showText(day);
            canvas2.endText();


            Image img = Image.getInstance(imagePath);
            PdfContentByte content = stamper.getOverContent(2);
            img.setAbsolutePosition(115, 581); // 坐标
            img.scaleToFit(80, 35); // 缩放
            content.addImage(img);
        }

        stamper.setFormFlattening(true);
        stamper.close();
        reader.close();
        outputStream.writeTo(Files.newOutputStream(Paths.get(outPath)));
        System.out.println("===============PDF签名成功=============");


    }

    public static void gdfaPDF(JSONObject param) throws IOException, DocumentException {

        int type = param.getInt("type");
        String inputPath = "";
        String outPath = "";
        if (type == 1) {
            inputPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\供电方案答复单模板1.pdf";
            outPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\供电方案答复单-1.pdf";
        } else if (type == 2) {
            inputPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\供电方案答复单模板2.pdf";
            outPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\供电方案答复单-2.pdf";
        } else if (type == 3) {
            inputPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\供电方案答复单模板3.pdf";
            outPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\PDF模板\\供电方案答复单-3.pdf";
        } else {
            System.out.println("不支持的模板类型");
            return;
        }

        byte[] inputData = FileUtil.readBytes(inputPath);
        PdfReader reader = new PdfReader(inputData);

        // 使用ByteArrayOutputStream来捕获PDF数据
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PdfStamper stamper = new PdfStamper(reader, outputStream);
        AcroFields form = stamper.getAcroFields();
        PdfContentByte canvas;
        BaseFont bf = BaseFont.createFont("Font/ybst.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        form.addSubstitutionFont(bf);

        List<String> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list.add("哈哈");
        }

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("fill_1", list.remove(0));
        data.put("fill_2", list.remove(0));
        data.put("fill_3", list.remove(0));
        data.put("fill_4", list.remove(0));
        data.put("fill_5", list.remove(0));
        data.put("fill_6", list.remove(0));
        data.put("fill_7", list.remove(0));
        data.put("fill_8", list.remove(0));
        data.put("fill_9", list.remove(0));
        data.put("fill_10", list.remove(0));
        data.put("fill_11", list.remove(0));
        data.put("fill_12", list.remove(0));
        data.put("fill_13", list.remove(0));
        data.put("fill_14", list.remove(0));
        data.put("fill_15", list.remove(0));
        data.put("fill_16", list.remove(0));
        data.put("fill_17", list.remove(0));
        data.put("fill_18", list.remove(0));
        data.put("fill_19", list.remove(0));

        if (type == 1) {
            canvas = stamper.getOverContent(1);
            canvas.beginText();
            canvas.setFontAndSize(bf, 9); // 字体和大小
            canvas.setTextMatrix(115, 49);
            canvas.showText("   年  月  日");
            canvas.endText();

        } else if (type == 2) {
            canvas = stamper.getOverContent(2);
            canvas.beginText();
            canvas.setFontAndSize(bf, 9); // 字体和大小
            canvas.setTextMatrix(115, 570);
            canvas.showText("   年  月  日");
            canvas.endText();

            data.put("fill_20", list.remove(0));
            data.put("fill_21", list.remove(0));
            data.put("fill_22", list.remove(0));
            data.put("fill_23", list.remove(0));
            data.put("fill_24", list.remove(0));
            data.put("fill_25", list.remove(0));
        } else {
            canvas = stamper.getOverContent(2);
            canvas.beginText();
            canvas.setFontAndSize(bf, 9); // 字体和大小
            canvas.setTextMatrix(115, 570);
            canvas.showText("   年  月  日");
            canvas.endText();

            data.put("fill_20", list.remove(0));
            data.put("fill_21", list.remove(0));
            data.put("fill_22", list.remove(0));
            data.put("fill_23", list.remove(0));
            data.put("fill_24", list.remove(0));
            data.put("fill_25", list.remove(0));
            data.put("fill_26", list.remove(0));
            data.put("fill_27", list.remove(0));
            data.put("fill_28", list.remove(0));
            data.put("fill_29", list.remove(0));
            data.put("fill_30", list.remove(0));
            data.put("fill_31", list.remove(0));
        }


        for (String key : data.keySet()) {
            form.setField(key, data.get(key).toString());
        }

        stamper.setFormFlattening(true);
        stamper.close();
        outputStream.writeTo(Files.newOutputStream(Paths.get(outPath)));
        outputStream.close();
        reader.close();

        System.out.println("===============PDF导出成功=============");

        // 获取生成的PDF字节数组
    }

    //    public static boolean incDate(LocalDate startDate, LocalDate endDate){
//        LocalDate now = LocalDate.now();
//        return  !now.isBefore(startDate) && !now.isAfter(endDate);
//    }
//
//    public static void wordToPdf() {
//        try {
//            WordprocessingMLPackage wordPackage = WordprocessingMLPackage.load(new File("E:\\shark\\需求文档\\2025-08-27 直通车\\申请单模板.docx"));
//            // 执行转换
//            Docx4J.toPDF(wordPackage, Files.newOutputStream(Paths.get("output.pdf")));
//            System.out.println("高级转换完成！");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
    // 生成供电方案答复单
    public static void generateSupplyPowerReplyForm() throws IOException {
        String docxPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\高压用电登记表-模板.docx";
        String pdfPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\高压用电登记表-模板1.docx";
        ByteArrayInputStream docxInputStream = new ByteArrayInputStream(FileUtil.readBytes(docxPath));
        ByteArrayOutputStream docxOutputStream = new ByteArrayOutputStream();
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            list.add("haha");
        }
        Map<String, Consumer<XWPFRun>> handlers = new HashMap<>();
        handlers.put("{date}", run -> run.setText(DateUtil.format(new Date(), "yyyy年MM月dd日"), 0));
        handlers.put("{}", run -> {
            run.setText(list.remove(0), 0);
        });
        handlers.put("{Wingdings}", run -> {
//            run.setText("☑", 0); //
            run.setText("☐", 0); //

        });


        XWPFDocument xwpfDocument = new XWPFDocument(docxInputStream);
//        insertMultiRowsWithStyle(xwpfDocument.getTables().get(0), 11, 13,1);
        xwpfDocument.getTables().get(0).getRows().stream()
                .flatMap(r -> r.getTableCells().stream())
                .flatMap(c -> c.getParagraphs().stream())
                .flatMap(p -> p.getRuns().stream())
                .forEach(run -> {
                    String text = run.getText(0);
                    System.out.println(text);
                    if (handlers.containsKey(text)) {
                        System.out.println(text);
                        handlers.get(text).accept(run);
                    }
                });
        xwpfDocument.write(docxOutputStream);
        docxOutputStream.writeTo(Files.newOutputStream(Paths.get(pdfPath)));
        xwpfDocument.close();
        docxOutputStream.close();
        docxInputStream.close();
    }

    public static XWPFTable insertMultiRowsWithStyle(XWPFTable table, int startRowIndex, int endRowIndex, int insertCount) {
        // 待复制的行
        List<XWPFTableRow> baseRows = new ArrayList<>();
        for (int i = startRowIndex; i <= endRowIndex; i++) {
            baseRows.add(table.getRow(i));
        }

        // 插入指定次数
        for (int n = 0; n < insertCount; n++) {
            int insertPos = endRowIndex + 1 + n * baseRows.size(); // 插入位置往后推移
            for (int j = 0; j < baseRows.size(); j++) {
                XWPFTableRow baseRow = baseRows.get(j);

                // 插入新行
                XWPFTableRow newRow = table.insertNewTableRow(insertPos + j);

                // 复制 CTRow 内容（包含样式、单元格等）
                CTRow ctRow = (CTRow) baseRow.getCtRow().copy();
                newRow.getCtRow().set(ctRow);
            }
        }
        return table;
    }

    // 生成业务办理申请单
    public static void generateBusinessHandlingApplicationForm() throws IOException {
        String docxPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\供电方案答复单模板.docx";
        String pdfPath = "E:\\shark\\需求文档\\2025-08-27 直通车\\供电方案答复单模板-3.docx";
        InputStream docxInputStream = Files.newInputStream(Paths.get(docxPath));
        OutputStream outputStream = Files.newOutputStream(Paths.get(pdfPath));
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            list.add("haha");
        }
        Map<String, Consumer<XWPFRun>> handlers = new HashMap<>();
        handlers.put("{date}", run -> run.setText(DateUtil.format(new Date(), "yyyy年MM月dd日"), 0));
        handlers.put("{}", run -> {
            run.setText(list.remove(0), 0);
        });

        XWPFDocument xwpfDocument = new XWPFDocument(docxInputStream);
        XWPFTable table = xwpfDocument.getTables().get(0);

        insertMultiRowsWithStyle(table, 12, 13, 1);

        table.getRows().stream()
                .flatMap(r -> r.getTableCells().stream())
                .flatMap(c -> c.getParagraphs().stream())
                .flatMap(p -> p.getRuns().stream())
                .forEach(run -> {
                    String text = run.getText(0);
                    if (handlers.containsKey(text)) {
                        handlers.get(text).accept(run);
                    }
                });

        xwpfDocument.write(outputStream);
        xwpfDocument.close();
        outputStream.close();
        docxInputStream.close();

    }


    // 获取Excel数据
    public static void test() {
        ExcelReader reader = ExcelUtil.getReader("E:\\shark\\需求文档\\2025-08-27 直通车\\行业.xlsx");

        // 获取从第二行开始（index=1，0是第一行）到最后一行的数据
        // 只取A到H列（索引0-7）
        List<List<Object>> rows = reader.read(1, reader.getRowCount());

        for (List<Object> row : rows) {
            // 截取A-H列数据
            List<Object> cells = row.subList(0, Math.min(8, row.size()));
            System.out.println(cells);
        }

        reader.close();
    }

    /// WORD转PDF
    public static void WordToPdfConverter() throws IOException {
        // 参考：https:blog.csdn.net/ka3p06/article/details/125476270 通过documents4j实现
        String docxPath = "C:\\Users\\xiechang\\Desktop\\FILE_DATA_1.docx";
        String pdfPath = "C:\\Users\\xiechang\\Desktop\\FILE_DATA_11.docx";
        InputStream docxInputStream = Files.newInputStream(Paths.get(docxPath));
        OutputStream outputStream = Files.newOutputStream(Paths.get(pdfPath));
        try {
            // 原word地址
//            docxInputStream = new FileInputStream("E:\\\\test2\\\\word模板.docx");
            // 转换后pdf生成地址
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream)
                    .as(DocumentType.DOCX)
                    .to(outputStream)
                    .as(DocumentType.PDF).execute();
            // 关闭
            converter.shutDown();
            // 关闭
            outputStream.close();
            // 关闭
            docxInputStream.close();
        } catch (Exception e) {
            System.out.println("[documents4J] word转pdf失败:" + e.toString());
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }
            if (docxInputStream != null) {
                docxInputStream.close();
            }
        }
    }

    /**
     * 在指定行下插入若干行，保持原行的样式（包括行高、单元格样式等）
     *
     * @param table        表格对象
     * @param baseRowIndex 基准行号（0-based）
     * @param insertCount  插入的行数
     */
    public static void insertRowsWithStyle(XWPFTable table, int baseRowIndex, int insertCount) {
        XWPFTableRow baseRow = table.getRow(baseRowIndex);

        for (int j = 0; j < insertCount; j++) {
            // 在指定位置插入新行
            XWPFTableRow newRow = table.insertNewTableRow(baseRowIndex + 1 + j);

            // 复制整行属性（包含行高等信息）
            CTRow ctRow = (CTRow) baseRow.getCtRow().copy();
            newRow.getCtRow().set(ctRow);

        }

    }

}
